<template>
  <view>
    <my-search @click="gotoSearch"></my-search>
    <view class="view-container">
      <!-- 左侧 -->
      <scroll-view class="left-scroll" scroll-y :style="{height: wh + 'px'}">
        <block v-for="(item,i) in cateList" :key="i">
          <view :class="['left-scroll-item', i === active ? 'active' : '']" @click="clickChange(i)" >{{item.cat_name}}</view>
        </block>
      </scroll-view>
        
      <!-- 右侧 -->
      <scroll-view class="right-scroll" scroll-y :style="{height: wh + 'px'}" :scroll-top="scrollTop">
        <view class="right-scroll-item" v-for="(ele,index2) in cateList[active]?.children" :key="index2">
          <view class="cate-lv2">/ {{ele.cat_name}} /</view>
          <view class="cate-lv3">
            <view class="cate-lv3-item" v-for="(item3, i3) in ele.children" :key="i3" @click="gotoGoods(item3)">
              <!-- 图片 -->
              <image :src="item3.cat_icon"></image>
              <!-- 文本 -->
              <text>{{item3.cat_name}}</text>
            </view>
          </view>
        </view>
      </scroll-view>
    </view>
  </view>
</template>

<script>
  import $http from '../../utils/request';
  export default {
    data() {
      return {
        //屏幕高度
        wh: 0,
        // 分类列表数据
        cateList: [],
        active: 0,
        scrollTop: 0,
      };
    },
    methods: {
      async getCateData() {
        const res = await uni.$http.get('api/public/v1/categories')
        this.cateList = res.data.message
      },
      //点击事件
      clickChange(i) {
        this.active = i
        this.scrollTop = Math.random(0,1)
      },
      // 跳转三级列表
      gotoGoods(item) {
        uni.navigateTo({
          url: '/subpkg/goods_list/goods_list?cid=' + item.cat_id
        })
      },
      gotoSearch(e) {
        // uni.navigateTo({
        //   url: '/subpkg/'
        // })
        console.log('ok',e);
      }
    },
    onLoad() {
      this.wh = uni.getSystemInfoSync().windowHeight
      this.getCateData()
    }
  }
</script>

<style lang="scss">
  .view-container {
    display: flex;

    .left-scroll {
      width: 120px;
      .left-scroll-item{
        background-color: #f7f7f7;
        line-height: 60px;
        text-align: center;
        font-size: 12;
        // 激活项的样式
              &.active {
                background-color: #ffffff;
                position: relative;
        
                // 渲染激活项左侧的红色指示边线
                &::before {
                  content: ' ';
                  display: block;
                  width: 3px;
                  height: 30px;
                  background-color: #c00000;
                  position: absolute;
                  left: 0;
                  top: 50%;
                  transform: translateY(-50%);
                }
              }
      }
    }

    .right-scroll {
      display: flex;
      
      .right-scroll-item{
        .cate-lv2 {
          text-align: center;
          font-weight: bold;
          font-size: 10;
          padding: 15rpx 0;
        }
        .cate-lv3 {
          display: flex;
          flex-wrap: wrap;
          .cate-lv3-item{
            width: 33.33%;
            border-bottom: 10px;
            display: flex;
            flex-direction: column;
            justify-content: center;
            align-items: center;
            image{
              width: 60px;
              height: 60px;
            }
            text {
              font-size: 12px;
            }
          }
        }
      }
    }
  }
</style>